package com.pioneer.beans;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

/*
 * Author: gchen
 * Created: Friday, February 20, 2004 5:51:32 PM
 * Modified: Friday, February 20, 2004 5:51:32 PM
 */


public class KeyGenerator
{
	public static String padBefore(String before, char padChar, int totalLength) {
		int toPad = totalLength - before.length();
		StringBuffer buf = new StringBuffer();
		if (toPad>0) {
			for (int count=0;count<toPad;count++) {
				buf.append(padChar);
			}			
		}
		buf.append(before);
		return buf.toString();
	}
	
	public static String getQuoteNum(String prevQuoteNum) {
		char lastYearDigit = prevQuoteNum.charAt(1);
		int lastSequence = Integer.parseInt(prevQuoteNum.substring(3,prevQuoteNum.length()));
		Calendar cal = new GregorianCalendar();
		cal.setTime(new Date());
		int year = cal.get(Calendar.YEAR);		
		char currYearDigit = Integer.toString(year).charAt(3);
		if (currYearDigit==lastYearDigit) {
			lastSequence++;
		}
		else {
			lastYearDigit = currYearDigit;
			lastSequence = 1;
		}
		String paddedSequence = padBefore(Integer.toString(lastSequence),'0',4);		
		return "Q"+lastYearDigit+"-"+paddedSequence;
	}
	
	public static void main(String args[]) {
		System.out.println(getQuoteNum("Q2-0001"));
	}

}
